package com.oocl.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.oocl.entity.Clients;
import com.oocl.entity.DeliverInfo;
import com.oocl.entity.Orders;
import com.oocl.service.ClientsService;
import com.oocl.service.DeliverInfoService;
import com.oocl.service.OrdersService;
import com.oocl.vo.OrdersVo;

@Controller
public class CommonController {

	@Resource(name="ordersService")
	private OrdersService ordersService;
	@Resource(name="clientsService")
	private ClientsService clientsService;
	@Resource(name="deliverInfoService")
	private DeliverInfoService deliverInfoService;
	
	/*
	 * @Method:toIndex
	 * @fuction:redirect to index page
	 * @params:Model model,HttpSession session
	 * @author:wendy
	 * @time:2017-08-14
	 */
	@RequestMapping(value="/toindex",method=RequestMethod.GET)
	public String toIndex(HttpSession ses,Model model){
		String cid=(String) ses.getAttribute("cid");
		model.addAttribute("cid", cid);
		return "welcome";
	}
	
	/*
	 * @Method:toOrderList
	 * @fuction:redirect to orderlist page
	 * @params:Model model,HttpSession session
	 * @author:wendy
	 * @time:2017-08-14
	 */
	@RequestMapping(value="/toorderlist",method=RequestMethod.GET)
	public String toOrderList(HttpSession ses,Model model){
		System.out.println("toorderlist come in");
		String cid=(String)ses.getAttribute("cid");
		Clients client=new Clients();
		client=clientsService.findClientsByCid(cid);
		List<Orders> olist=new ArrayList<Orders>();
		olist=ordersService.loadOrdersByCid(cid);
		List<OrdersVo> ovlist=new ArrayList<OrdersVo>();
		for(Orders order:olist){
			OrdersVo ordervo=new OrdersVo(order.getOtime(), 
					order.getMerchant().getMname(), 
					order.getOid(), 
					order.getOstate(), 
					order.getOsum());
			ovlist.add(ordervo);
		}
		model.addAttribute("ovlist", ovlist);
		return "orderslist";
	}
	
	/*
	 * @Method:toAddress
	 * @fuction:redirect to addresslist page
	 * @params:Model model,HttpSession session
	 * @author:wendy
	 * @time:2017-08-14
	 */
	@RequestMapping(value="/toaddress",method=RequestMethod.GET)
	public String toAddress(HttpSession ses,Model model){
		String cid=(String)ses.getAttribute("cid");
		List<DeliverInfo> dlist=new ArrayList<DeliverInfo>();
		dlist=deliverInfoService.loadAllDeliverInfoByCid(cid);
		model.addAttribute("dlist", dlist);
		return "address";
	}
	
	/*
	 * @Method:toLogout
	 * @fuction:logout and redirect to index page
	 * @params:HttpSession session
	 * @author:wendy
	 * @time:2017-08-14
	 */
	@RequestMapping(value="/tologout",method=RequestMethod.GET)
	public String toLogout(HttpSession ses){
		ses.removeAttribute("cart");
		ses.removeAttribute("cid");
		ses.removeAttribute("icnum");
		ses.removeAttribute("cname");
		return "login";
	}
	@RequestMapping(value="/tologin",method=RequestMethod.GET)
	public String toLogin(){
		return "login";
	}
	@RequestMapping(value="/toregist",method=RequestMethod.GET)
	public String toRegist(){
		return "regist";
	}
}
